﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using VicMessenger.BLL;
using VicMessenger.DB;
using VicMessenger.Common;


namespace VicMessenger
{
    /// <summary>
    /// Interaction logic for Campaign.xaml
    /// </summary>
    public partial class Campaign : Window
    {
        proc_Campaign_SelectResult globalCampaign;
        proc_Category_SelectResult globalCategory;

        bool globalInsertFlag = false;

        string globalCurrentSelectedSection = string.Empty;

        public Campaign()
        {
            InitializeComponent();

            Loaded += new RoutedEventHandler(Campaign_Loaded);
            treeCampaign.SelectedItemChanged += new RoutedPropertyChangedEventHandler<object>(TreeCampaign_SelectedItemChanged);
        }

        void TreeCampaign_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            //insert flag false
            globalInsertFlag = false;

            TreeViewItem selItem = treeCampaign.SelectedItem as TreeViewItem;

            foreach (var item in RootLayout.Children)
            {
                if (item is TextBox)
                {
                    TextBox txt = item as TextBox;
                    txt.Text = "";
                }
            }

            txtName.Text = "";
            txtDescription.Text = "";

            if (selItem != null)
            {
                globalCurrentSelectedSection = string.Empty;
                int _selID;
                int.TryParse(selItem.Tag.ToString(), out  _selID);


                if (selItem.Tag.ToString().Length > 0)
                {
                    switch (selItem.ToolTip.ToString().ToLower())
                    {
                        case "root":
                            globalCurrentSelectedSection = "root";
                            break;
                        case "category":
                            globalCurrentSelectedSection = "category";
                            globalCategory = CategoryBLL.GetCategoryByID(_selID);
                            txtName.Text = globalCategory.CategoryName;
                            txtDescription.Text = globalCategory.Description;
                            CommonCls.GlobalCategoryId = globalCategory.CategoryID;

                            break;
                        case "campaign":

                            globalCurrentSelectedSection = "campaign";
                            globalCampaign = CampaignBLL.GetCampaignByID(_selID);
                            txtName.Text = globalCampaign.CampaignName;
                            txtDescription.Text = globalCampaign.Description;
                            CommonCls.GlobalCampaignId = globalCampaign.CampaignID;


                            break;
                        default:
                            break;
                    }

                }


            }
        }

        void Campaign_Loaded(object sender, RoutedEventArgs e)
        {
            PopulateTreeView();





        }



        private void PopulateTreeView()
        {

            treeCampaign.Items.Clear();

            List<proc_Category_SelectResult> retCategory = CategoryBLL.GetAllCategory();

            TreeViewItem rootItem = new TreeViewItem();
            rootItem.Header = "Root";
            rootItem.Tag = "root";
            rootItem.ToolTip = "Root";
            rootItem.IsExpanded = true;
            treeCampaign.Items.Add(rootItem);

            foreach (proc_Category_SelectResult item in retCategory)
            {
                TreeViewItem titem = new TreeViewItem();
                titem.Header = item.CategoryName;
                titem.Tag = item.CategoryID;
                titem.ToolTip = "Category";
                titem.IsExpanded = true;
                rootItem.Items.Add(titem);

                List<proc_Campaign_SelectResult> retCampaignCol = CampaignBLL.GetCampaignByCategoryId(item.CategoryID);

                foreach (proc_Campaign_SelectResult itemCategory in retCampaignCol)
                {
                    TreeViewItem titem_campaign = new TreeViewItem();
                    titem_campaign.Header = itemCategory.CampaignName;
                    titem_campaign.Tag = itemCategory.CampaignID;
                    titem_campaign.ToolTip = "Campaign";
                    titem_campaign.IsExpanded = true;
                    titem.Items.Add(titem_campaign);

                }

            }
        }



        private void treeCampaign_MouseRightButtonUp(object sender, MouseButtonEventArgs e)
        {
            UIElement ClickedItem = VisualTreeHelper.GetParent(e.OriginalSource as UIElement) as UIElement;

            if (ClickedItem == null) return;

            //find the clicked TreeViewItem 
            while ((ClickedItem != null) && !(ClickedItem is TreeViewItem))
            {
                ClickedItem = VisualTreeHelper.GetParent(ClickedItem) as UIElement;


            }

            if (ClickedItem != null)
            {
                TreeViewItem _item = ClickedItem as TreeViewItem;
                _item.IsSelected = true;

                if (_item.ToolTip.ToString() == "Category")
                {
                    ContextMenu mnu = CategoryContextMenu();
                    _item.ContextMenu = mnu;
                    _item.ContextMenu.IsOpen = true;
                }
                else if (_item.ToolTip.ToString() == "Campaign")
                {
                    ContextMenu mnu = CampaignContextMenu();
                    _item.ContextMenu = mnu;
                    _item.ContextMenu.IsOpen = true;
                }
                else if (_item.ToolTip.ToString() == "Root")
                {
                    ContextMenu mnu = RootContextMenu();
                    _item.ContextMenu = mnu;
                    _item.ContextMenu.IsOpen = true;
                }
            }

            e.Handled = true;
        }



        #region "Save Data"
        //===============================Save Data =======================================

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            if (globalCurrentSelectedSection == null || globalCurrentSelectedSection.Trim() == "") return;


            switch (globalCurrentSelectedSection)
            {
                case "category":
                    SaveCategory();
                    break;
                case "campaign":
                    SaveCampaign();
                    break;
                default:
                    break;
            }

            //insert flag
            globalInsertFlag = false;

        }

        private void SaveCampaign()
        {
            bool againPopulate = false;

            if (globalCampaign != null)
            {
                //update
                if ((globalCampaign.CampaignName != txtName.Text) || (globalCampaign.Description != txtDescription.Text))
                {
                    //refresh treeview item only if name changes
                    if (globalCampaign.CampaignName != txtName.Text)
                    {
                        againPopulate = true;
                    }

                    globalCampaign.CampaignName = txtName.Text.Trim();
                    globalCampaign.Description = txtDescription.Text.Trim();
                    if (CampaignBLL.InsertUpdateCampaign(globalCampaign) > 0)
                    {
                        if (againPopulate == true)
                        {
                            PopulateTreeView();
                            txtName.Text = globalCampaign.CampaignName;
                            txtDescription.Text = globalCampaign.Description;
                        }
                        MessageBox.Show("Campaign Saved!");
                    }
                }
            }
            else
            {
                //insert
                if (globalInsertFlag == true)
                {
                    globalCampaign = new proc_Campaign_SelectResult();
                    globalCampaign.CampaignID = 0;
                    globalCampaign.CampaignName = txtName.Text.Trim();
                    globalCampaign.Description = txtDescription.Text.Trim();
                    globalCampaign.CategoryID = CommonCls.GlobalCategoryId;
                    CampaignBLL.InsertUpdateCampaign(globalCampaign);


                    PopulateTreeView();
                    txtName.Text = globalCampaign.CampaignName;
                    txtDescription.Text = globalCampaign.Description;
                }

            }

        }

        private void SaveCategory()
        {
            bool againPopulate = false;

            if (globalCategory != null)
            {
                if ((globalCategory.CategoryName != txtName.Text) || (globalCategory.Description != txtDescription.Text))
                {
                    //refresh treeview item only if name changes

                    if (globalCategory.CategoryName != txtName.Text)
                    {
                        againPopulate = true;
                    }

                    globalCategory.CategoryName = txtName.Text.Trim();
                    globalCategory.Description = txtDescription.Text.Trim();
                    if (CategoryBLL.InsertUpdateCategory(globalCategory) > 0)
                    {
                        if (againPopulate == true)
                        {
                            PopulateTreeView();
                            txtName.Text = globalCategory.CategoryName;
                            txtDescription.Text = globalCategory.Description;
                        }
                        MessageBox.Show("Category Saved!");
                    }
                }
            }
            else
            {
                //insert
                if (globalInsertFlag == true)
                {
                    globalCategory = new proc_Category_SelectResult();
                    globalCategory.CategoryID = 0;
                    globalCategory.CategoryName = txtName.Text.Trim();
                    globalCategory.Description = txtDescription.Text.Trim();
                    CategoryBLL.InsertUpdateCategory(globalCategory);


                    PopulateTreeView();
                    txtName.Text = globalCategory.CategoryName;
                    txtDescription.Text = globalCategory.Description;
                }
            }

        }
        //===============================Save Data =======================================
        #endregion

        #region "Context Menu"
        //===============================Context Menu =======================================
        private ContextMenu CategoryContextMenu()
        {
            ContextMenu mainMenu = new ContextMenu();
            //txtControl.ContextMenu = mainMenu;

            MenuItem itemAdd = new MenuItem();
            itemAdd.Header = "Add Campaign";
            itemAdd.Foreground = Brushes.Black;
            itemAdd.Background = Brushes.Transparent;
            itemAdd.Tag = "add_campaign";
            itemAdd.Click += new RoutedEventHandler(mnuItemClick);
            mainMenu.Items.Add(itemAdd);

            ////create image at runtime and attach to menu at runtime
            //BitmapImage copyimage = new BitmapImage();
            //copyimage.BeginInit();
            //Uri myUri = new Uri("Copy.png", UriKind.RelativeOrAbsolute);
            //copyimage.UriSource = myUri;
            //copyimage.EndInit();

            //Image iconImage = new Image();
            //iconImage.Source = copyimage;

            //MenuItem item3 = new MenuItem();
            //item3.Header = "Copy";
            ////item3.Icon = iconImage;
            //mainMenu.Items.Add(item3);

            return mainMenu;

        }

        private ContextMenu CampaignContextMenu()
        {
            ContextMenu mainMenu = new ContextMenu();
            //txtControl.ContextMenu = mainMenu;

            MenuItem itemDel = new MenuItem();
            itemDel.Header = "Delete";
            itemDel.Foreground = Brushes.Black;
            itemDel.Background = Brushes.Transparent;
            itemDel.Tag = "del_campaign";
            itemDel.Click += new RoutedEventHandler(mnuItemClick);
            mainMenu.Items.Add(itemDel);




            return mainMenu;

        }

        private ContextMenu RootContextMenu()
        {
            ContextMenu mainMenu = new ContextMenu();
            //txtControl.ContextMenu = mainMenu;

            MenuItem itemAdd = new MenuItem();
            itemAdd.Header = "Add Category";
            itemAdd.Foreground = Brushes.Black;
            itemAdd.Background = Brushes.Transparent;
            itemAdd.Click += new RoutedEventHandler(mnuItemClick);
            itemAdd.Tag = "add_category";

            mainMenu.Items.Add(itemAdd);




            return mainMenu;

        }

        void mnuItemClick(object sender, RoutedEventArgs e)
        {
            MenuItem _item = sender as MenuItem;

            if (_item != null)
            {
                txtName.Text = "";
                txtDescription.Text = "";


                if (_item.Tag.ToString() == "add_category")
                {
                    txtName.Focus();
                    globalCategory = null;
                    //insert flag
                    globalInsertFlag = true;
                    globalCurrentSelectedSection = "category";
                }


                if (_item.Tag.ToString() == "add_campaign")
                {
                    txtName.Focus();
                    globalCampaign = null;
                    //insert flag
                    globalInsertFlag = true;
                    globalCurrentSelectedSection = "campaign";
                }


                if (_item.Tag.ToString() == "del_campaign")
                {
                    globalCampaign = null;
                    globalCategory = null;

                    //insert flag
                    globalInsertFlag = false;
                    globalCurrentSelectedSection = "campaign";

                    CampaignBLL.DeleteCampaign(CommonCls.GlobalCampaignId);

                    PopulateTreeView();
                    MessageBox.Show("Campaign Deleted");

                }

            }

        }

        //===============================Context Menu =======================================
        #endregion


    }
}
